﻿/**
 * ...
 * @author Артём Григорьев
 */

package tab;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.Lib;
import flash.utils.Timer;
import gui.hug.HUGSprite;
import gui.hug.HUGTabbedPanel;
import gui.hug.Tab;
import gui.hug.HUGCheckBox;
import list.FriendsPanel;

import list.NewList;
import list.SearchPanel;
import list.OpenTabEvent;

import loader.Loader;

import client.Channel;
import client.Item;
import client.ReceivedItem;

import haxe.Timer;


class MainTab extends Tab
{
	private static inline var 	TITLE = "Главная";
	
	private var loader 			: Loader;
	private var newsList 		: NewList;
	private var search			: SearchPanel;
	private var addFriends 		: FriendsPanel;
	private var updateTime	    : String;
	private var tm 				: haxe.Timer; 
	private var sprite			: HUGSprite;
	private var panelS 			: HUGTabbedPanel;
	private var autoUpdt 		: HUGCheckBox;
	
	
	public function new(panel : HUGTabbedPanel = null, uid : Int ) 
	{
		this.panelS = panel;
		sprite = new HUGSprite();
		var me = this;
		
		// initialization
		Loader.createInstance();
		loader = Loader.getInstance();
		
		autoUpdt = new HUGCheckBox("Автообновление", 250, 13);
		autoUpdt.setChecked(false);
		autoUpdt.addEventListener(MouseEvent.CLICK, onUpdtCheckBox);
		// creating search		
		
		search = new SearchPanel(187, 0, function (text: String) { 
		    if (text != "")	me.getPanel().addTab(new FindTab(text, me.panel));	
		});		
		
		var loadMessageCount = function (count : Int) {
			me.addFriends = new FriendsPanel(187, 0, function () {  
				Application.showInviteBox();
			}, function (items: Array < ReceivedItem > ) {
					  me.getPanel().addTab(new MailTab(items)); }, count );	
					  me.sprite.addChild(me.addFriends);
					  me.addFriends.startTimer();
		}
		loader.messagesToRead(loadMessageCount);
		
		
		
		newsList = new NewList(0, Std.int(search.height) + 20, 600);
		tm = new haxe.Timer(2000);
		// creating layout
		sprite.addChild(search);
		sprite.addChild(newsList);
		sprite.addChild(autoUpdt);
		
		// loading data from server
		loader.getChannels(onLoad);
		
		// listen for opening new tabs
		var onOpenTabEvent = function(e : OpenTabEvent) {
			var channel : Channel = cast(e.getContent(), Channel);
			panel.addTab(new ItemsTab(channel.getChanId(), channel.getName(), panel, uid)); 
		};
		newsList.addEventListener(OpenTabEvent.OPEN_TAB, onOpenTabEvent);
		updateTime = DateTools.format(Date.now(), "%Y-%m-%d %H:%M:%S");
		
		tm.run = startTimer;
		super(sprite, TITLE,0, panel, false);
		
	}
	
	public function onLoad(data : Array<Channel>) : Void {
		for (channel in data) {
			newsList.addItem(channel,0);
		}
	}
	
	private function onRefresh(s : String) {
		if (s == "yes") {
			sprite.removeChild(newsList);
			newsList.clearContent();
			loader.getChannels(onLoad);
			sprite.addChild(newsList);
			updateTime = DateTools.format(Date.now(), "%Y-%m-%d %H:%M:%S");
		}
	}
	
	private function startTimer() : Void {
		loader.updtTime(updateTime, onRefresh);
	}
	public function UpdLoad(data: Array<Channel>): Void
	{
		var len = Std.int(newsList.getLength());
		var i:Int = 0;
		for (i in 0...len - 1)
		newsList.deleteItem(i);
		for (channel in data)
		{   
			newsList.addItem(channel,0);
		}
		
	}
	public function Refresh() {
	    loader.getChannels(UpdLoad);
	}
	
	private function onUpdtCheckBox(e : MouseEvent) {
		if (autoUpdt.getChecked()) {
			tm = new haxe.Timer(2000);
			tm.run = startTimer;			
		} else {
			//tm.run = null;
			tm.stop();
		}
	}
}